package com.jpxx.netty.helloAandB;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class LiaoTian {
	static final String HOST = System.getProperty("host", "127.0.0.1");  
	static final int PORT = Integer.parseInt(System.getProperty("port", "8080"));  
	static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));  
	
	
	public static void liaoTian(String message) throws Exception{
		// Configure the client.  
				EventLoopGroup group = new NioEventLoopGroup();  
				try {  
					Bootstrap b = new Bootstrap();  
					b.group(group)  
					.channel(NioSocketChannel.class)  
					.option(ChannelOption.TCP_NODELAY, true)  
					.handler(new ChannelInitializer<SocketChannel>() {  
						@Override  
						public void initChannel(SocketChannel ch) throws Exception {  
							ch.pipeline().addLast("decoder", new StringDecoder());  
							ch.pipeline().addLast("encoder", new StringEncoder());  
							ch.pipeline().addLast(new ClientHandler());  
						}  
					});  

					ChannelFuture future = b.connect(HOST, PORT).sync(); //绑定服务端
					
					future.channel().writeAndFlush(message);  //发送内容
					
					future.channel().closeFuture().sync();  
				} finally {  
					group.shutdownGracefully();
				}  
	}
	public static void main(String[] args) throws Exception {  
		liaoTian("我是张宁,你叫什么名字3333");
	}  

}
